package com.childmeter.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.childmeter.entity.JsonChildmeter;
import com.entity.ChildMeter;
import com.entity.Meter;
import com.file.entity.FileMeter;
import com.file.service.MeterService;
import com.interfaces.ChildMeterDao;
import com.interfaces.MeterDao;
import com.utils.JsonResult;

/**
 * @Title: ChildmeterService.java
 * @Package com.childmeter.service
 * @author 孙梦楠
 * @date 2017年10月16日 上午11:26:30
 * @version V1.0.0
 */
@Service
public class ChildmeterService {

	@Autowired
	private ChildMeterDao childmeterdao;

	@Autowired
	private MeterDao meterdao;

	@Autowired
	private MeterService meterervice;

	// 列出所有没有设置过总分类型的表列表
	@Transactional
	public JsonResult<List<FileMeter>> listmeter2(String opeorgcookie, String opetypecookie, String languagecookie) {
		List<FileMeter> fileMeters = new ArrayList<FileMeter>();
		List<Meter> meterlist = meterdao.listmeter2(Integer.valueOf(opeorgcookie), opetypecookie);
		for (Meter meter : meterlist) {
			fileMeters.add(meterervice.dofilemeter(meter, languagecookie));
		}
		return new JsonResult<List<FileMeter>>(721, "列表所有可设置的表", fileMeters);
	}

	// 列出符合org的所有未连接总表
	@Transactional
	public JsonResult<List<FileMeter>> listfathermeter(String opeorgcookie, String opetypecookie,
			String languagecookie) {
		List<FileMeter> fileMeters = new ArrayList<FileMeter>();
		List<Meter> meterlist = meterdao.listfathermeter(Integer.valueOf(opeorgcookie), opetypecookie);
		for (Meter meter : meterlist) {
			fileMeters.add(meterervice.dofilemeter(meter, languagecookie));
		}
		return new JsonResult<List<FileMeter>>(722, "列出符合org的所有未连接总表", fileMeters);
	}

	// 列出符合org的所有已连接总表
	@Transactional
	public JsonResult<List<FileMeter>> listconnectedfathermeter(String opeorgcookie, String opetypecookie,
			String languagecookie) {
		List<FileMeter> fileMeters = new ArrayList<FileMeter>();
		List<Meter> meterlist = meterdao.listconnectedfathermeter(Integer.valueOf(opeorgcookie), opetypecookie);
		for (Meter meter : meterlist) {
			fileMeters.add(meterervice.dofilemeter(meter, languagecookie));
		}
		return new JsonResult<List<FileMeter>>(724, "列出符合org的所有已连接总表", fileMeters);
	}

	// 列出符合org的所有未连接分表
	@Transactional
	public JsonResult<List<FileMeter>> listchildmeter(String opeorgcookie, String opetypecookie,
			String languagecookie) {
		List<FileMeter> fileMeters = new ArrayList<FileMeter>();
		List<Meter> meterlist = meterdao.listchildmeter(Integer.valueOf(opeorgcookie), opetypecookie);
		for (Meter meter : meterlist) {
			fileMeters.add(meterervice.dofilemeter(meter, languagecookie));
		}
		return new JsonResult<List<FileMeter>>(723, "列出符合org的所有分表", fileMeters);
	}

	// 设置总分表
	@Transactional
	public JsonResult<ChildMeter> setFatherChildmeter(List<ChildMeter> meters, String type) {

		for (ChildMeter meter : meters) {
			ChildMeter childMeter = new ChildMeter();
			Integer meterid = meter.getMeterid();
			childMeter.setMeterid(meterid);
			childMeter.setType(type);// 总表是1 分表是2
			if (type.equals("1")) {
				childMeter.setFathermeterid(0);
			}
			//只有没设置过才可以设置
			if (childmeterdao.findByMeterid(meterid) == null) {
				childmeterdao.save(childMeter);
			}
		}

		return new JsonResult<ChildMeter>(725, "设置成功", null);
	}

	// 清除总分表设置
	@Transactional
	public void delect1(Integer meterid, String type) {
		// 总表 如果没有分表 直接删除 如果有 删除关联关系再删除
		if (type.equals("1")) {
			if (childmeterdao.findByFathermeterid(meterid).size() == 0) {
				childmeterdao.delete(childmeterdao.findByMeterid(meterid));
			} else {
				List<ChildMeter> childMeters = childmeterdao.findByFathermeterid(meterid);
				for (ChildMeter childMeter : childMeters) {
					childmeterdao.updateMeter(null, childMeter.getMeterid());
				}
				childmeterdao.delete(childmeterdao.findByMeterid(meterid));
			}
			// 分表 直接删除
		} else if (type.equals("2")) {
			childmeterdao.delete(childmeterdao.findByMeterid(meterid));
		}

	}

	// ===========

	// 关联总分表
	@Transactional
	public JsonResult<ChildMeter> connect(Integer fathermeterid, List<ChildMeter> meters) {

		for (ChildMeter meter : meters) {
			Integer meterid = meter.getMeterid();
			childmeterdao.updateMeter(fathermeterid, meterid);
		}
		childmeterdao.updateMeter(0, fathermeterid);

		return new JsonResult<ChildMeter>(726, "关联成功", null);
	}

	// 清除分表关联
	@Transactional
	public JsonResult<ChildMeter> delect(Integer meterid) {

		if (childmeterdao.findOne(meterid) != null) {
			childmeterdao.updateMeter(null, meterid);

		}
		return new JsonResult<ChildMeter>(727, "清除分表关联成功", null);
	}

	// 小区下的总分表结构
	@Transactional
	public JsonResult<List<JsonChildmeter>> listFatherAndChildMeterByArea(String opeorgcookie, String opetypecookie,
			String languagecookie) {
		List<JsonChildmeter> Jsonmeters = new ArrayList<JsonChildmeter>();
		List<Meter> fathermeters = meterdao.listconnectedfathermeter(Integer.valueOf(opeorgcookie), opetypecookie);
		for (Meter meter : fathermeters) {
			JsonChildmeter fathermeter = new JsonChildmeter();
			fathermeter.setMeterid(meter.getMeterid());
			fathermeter.setType("1");
			Jsonmeters.add(fathermeter);
			if (childmeterdao.findByFathermeterid(meter.getMeterid()).size() != 0) {
				List<ChildMeter> childMeters = childmeterdao.findByFathermeterid(meter.getMeterid());
				for (ChildMeter childMeter : childMeters) {
					JsonChildmeter fathermeter1 = new JsonChildmeter();
					fathermeter1.setMeterid(childMeter.getMeterid());
					fathermeter1.setType("2");
					Jsonmeters.add(fathermeter1);
				}
			}
		}
		return new JsonResult<List<JsonChildmeter>>(728, "列出符合org的所有总分表", Jsonmeters);
	}

	// 根据总表列出下面的分表
	@Transactional
	public JsonResult<List<JsonChildmeter>> listChildMeterByFatherMeter(Integer fathermeterid) {
		List<JsonChildmeter> Jsonmeters = new ArrayList<JsonChildmeter>();
		if (childmeterdao.findByFathermeterid(fathermeterid).size() != 0) {
			List<ChildMeter> childMeters = childmeterdao.findByFathermeterid(fathermeterid);
			for (ChildMeter childMeter : childMeters) {
				JsonChildmeter fathermeter = new JsonChildmeter();
				fathermeter.setMeterid(childMeter.getMeterid());
				fathermeter.setType("2");
				Jsonmeters.add(fathermeter);
			}
		}
		return new JsonResult<List<JsonChildmeter>>(729, "列出单个总表下的的所有分表", Jsonmeters);
	}

}
